'use strict';

export class httpSeaFile {
    constructor() {
    }

    //get请求
    static get(url, successCallback, failureCallBack, errorCallback) {
        axios({
            url: url,
            method: "GET",
            timeout: 60000,
            headers: {"Accept": "application/json; indent=4"},
        }).then(function (response) {
            handler(response, successCallback, failureCallBack)
        }).catch(errorCallback);
    }
    //附带 basic auth的post 请求
    static post(url, data, successCallback, failureCallBack, errorCallback) {

        axios({
            url: url,
            method: "POST",
            timeout: 60000,
            headers: {"Content-Type": "application/json"},
            data: data,
        }).then(function (response) {
            handler(response, successCallback, failureCallBack)
        }).catch(errorCallback);
    }
    //无basic auth的post 请求
    static post_(url, data, successCallback, failureCallBack, errorCallback) {
        axios({
            url: url,
            method: "POST",
            timeout: 60000,
            headers: {"Content-Type": "application/json"},
            data: data,
        }).then(function (response) {
            setTocken(response, successCallback, failureCallBack)
        }).catch(errorCallback)

    }

    //附带 basic auth的put 请求
    static put(url, data, successCallback, failureCallBack, errorCallback) {
        axios({
            url: url,
            method: "PUT",
            timeout: 60000,
            headers: {"Content-Type": "application/json", },
            data: data,
        }).then(function (response) {
            handler(response, successCallback, failureCallBack)
        }).catch(errorCallback);
    }
}

//处理http请求返回的response
function handler(response, successCallback, failureCallBack) {
    console.log("response==========>",response);
    if (response.status === 200) {
            successCallback(response.data)
    } else {
        failureCallBack(response.status);
    }
}

function setTocken(response, successCallback, failureCallBack) {
    console.log("response",response)
    if (response.status === 200) {
            successCallback(response.data)
    } else {
        failureCallBack(response.status);
    }
}






